#!/bin/sh

# setup_user_environment: Check to see if there's an old user folder; if there
#                         is, use that.

OLD_USER_FOLDER="/Users/.old"

# The domain on which users are defined
DOMAIN="/LDAPv3/ldap.itd.umich.edu"

user="${1}"

# If the username is blank, exit with an error.
if [ -z "${user}" ]; then
	logger -is "Username is zero-length."
	exit 1
fi

# If the user is defined in the local DirectoryServices domain, don't manage it
if [ "$(dscl . -read /Users/${user})" ]; then
	exit 0
fi

# Make sure the old user folder exists.
if [ ! -d "${OLD_USER_FOLDER}" ]; then
	mkdir -p "${OLD_USER_FOLDER}"
fi

userHome="$(dscl ${DOMAIN} -read /Users/${user} NFSHomeDirectory | \
	    awk '{ print $NF }' | sed -e 's/users/Users/g')"

oldUserHome="${OLD_USER_FOLDER}/${user}"

# If there's a folder in the old users folder for the user, move that to the
# correct folder. Otherwise, let the system handle it.
if [ -d "${oldUserHome}" ]; then #there is an old folder
	rm -rf "${userHome}"
	mv -f "${oldUserHome}" "${userHome}"
fi
